<?php
namespace app\index\model;

use app\admin\model\Column;
use app\admin\model\AdItem;
use app\admin\model\AdPosition;

class tags extends \think\template\TagLib
{
    protected $tags =   [
                // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
            'ads'               =>  ['attr' => 'id','close' =>  0 ],
            'columns'           =>  ['attr' => 'id,field,limit','close'=>1],
            'columnchild'       =>  ['attr' => 'id,field,limit','close'=>1],
            'navs'              =>  ['attr' => 'field,limit','close'=>1],
            'friends'           =>  ['attr' => 'field,limit','close'=>1],
    ];

    /** 导航 条
     * @param $tag
     * @param $content
     */
    public function tagNavs($tag,$content){
        $out     =   '<?php ';
        $out    .=  '$data = Db("column")->where(["status"=>1,"pid"=>0,"is_nav"=> 1])->order("sort desc")->limit('.$tag['limit'].')->field("'.$tag['field'].'")->select();';
        $out    .=  'foreach($data as $v):';
        $out    .=  'extract($v);';
        $out    .=  '$url = url("index/index"); ?>';
        $out    .=  $content;
        $out    .=  '<?php endforeach ?>';
        return $out;
    }

    /** 友情链接
     * @param $tag
     * @param $content
     * @return string
     */
    public function tagFriends($tag,$content){
        $out     =   '<?php ';
        $out    .=  '$data = Db("friendship_link")->where(["status"=>1])->order("sort desc")->limit('.$tag['limit'].')->field("'.$tag['field'].'")->select();';
        $out    .=  'foreach($data as $v):';
        $out    .=  'extract($v);';
        $out    .=  ' ?>';
        $out    .=  $content;
        $out    .=  '<?php endforeach ?>';
        return $out;
    }
    /**
     * columns
     */
    public function tagColumns($tag,$content){
        $column_id  =   $tag['id'];
        $column = Column::get(['column_id'=>$column_id],'model');
        $out     =   '<?php ';
        $out    .=  '$data = Db("'.$column->model->tablename.'")->where(["status"=>1,"recycle"=>0,"column_id"=>'.$column_id.'])->order("sort desc")->limit('.$tag['limit'].')->field("'.$tag['field'].',(\''.$column->alias.'\') as column_alias,('.$column->model->tablename.'_id'.') as id")->select();';
        $out    .=  'foreach($data as $v):';
        $out    .=  'extract($v);';
        $out    .=  '$url = url("index/index"); ?>';
        $out    .=  $content;
        $out    .=  '<?php endforeach ?>';
        return $out;
    }
    /**
     * columns child
     */
    public function tagColumnchild($tag,$content){
        $column_id  =   $tag['id'];

        $out     =   '<?php ';
        $out    .=  '$data = Db("column")->where(["status"=>1,"pid"=>'.$column_id.'])->order("sort desc")->limit('.$tag['limit'].')->field("'.$tag['field'].'")->select();';
        $out    .=  'foreach($data as $v):';
        $out    .=  'extract($v);';
        $out    .=  '$url = url("index/index"); ?>';
        $out    .=  $content;
        $out    .=  '<?php endforeach ?>';
        return $out;
    }
    /** 广告
     * @param $tag
     * @return string
     */
    public function tagAds($tag){
        // 获取 广告 分组 id 并且获取此分组信息
        $id =   $tag['id'];
        $adPosition = AdPosition::get($id);
        if (empty($adPosition)){
            return '<li>无此广告位！</li>';
        }
        // 模板 addslashes
        $template   =   addslashes(htmlspecialchars_decode($adPosition['template']));
        //广告宽
        $width      =   $adPosition['width'];
        //广告 高
        $height     =   $adPosition['height'];
        //广告数量
        $view_num   =   $adPosition['view_num'];
        // 通过 广告 分组 查找 此 分组下 所有广告
        $item = AdItem::where(['ad_position_id'=>$adPosition->ad_position_id,'status'=>1,'end_time'=>['gt',time()]])
                        ->limit($view_num)
                        ->select();
        if (empty($item)){
            return '<li>此广告位,无广告！</li>';
        }
        $output =   '';
        foreach ($item as $v){
            $title  =   $v['title'];
            $content=   $v['content'];
            $url    =   $v['url'];
            $output .=  '<li>';
            eval('$output .= ("'.$template.'");');
            $output .=  "</li>";
        }
//        unserialize()
//         $html    =   '<?php ';
//         $html    .=  "\"".serialize($item)."\"";
//         $html    .=  'foreach (unserialize($item) as $k=>$v){ ';
//         $html    .=  '$title      =   $v[\'title\'];';
//         $html    .=  '$content    =   $v[\'content\'];';
//         $html    .=  '$url        =   $v[\'url\'];';
//         $html    .=  '$output =   "<li>";';
//         $html    .=  '$output =   "{$template}";';
//         $html    .=  '$output =   "</li>";';
//         $html    .=  'echo $output;';
//
        return $output;
    }
}